import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

/**
 * @method
 * @description vuex持久化插件
 * @param {Store} store  Store对象
 */
const storePersist = (store) => {
  const key = 'VUEX_Persist';
  const storage = window.sessionStorage;
  //取数
  const data = storage.getItem(key);
  data && store.replaceState(JSON.parse(data))
  //存储
  store.subscribe(function(mutation,state){
    console.log(mutation)
    storage.setItem(key, JSON.stringify(state))
  })
}

export default new Vuex.Store({
  state: {
    /**
     * 用户信息
     */
    user:{},
    /**
     * 激活的菜单id
     */
    activeMenuId:0,
  },
  getters: {
    /**
     * token
     */
    token(state){
      return state.user.token;
    },
    /**
     * 菜单
     */
    tree(state){
      return state.user.tree;
    },
  },
  mutations: {
    /**
     * 设置用户信息
     */
    setUser(state,payload){
      state.user = payload
    },
    /**
     * 设置激活的菜单id
     */
    setActiveMenuId(state,payload){
      state.activeMenuId=payload;
    }
  },
  actions: {
    // /**
    //  * 退出
    //  */
    // exit({commit}){
    //   commit("setUser",{})
    //   commit("setMenus",{})
    //   router.push({name:"Login"})
    // }
  },
  modules: {
  },
  plugins:[storePersist]
})
